package com.java110.mall.cart.api;

import com.alibaba.fastjson.JSONObject;
import com.java110.mall.cart.bmo.inoutOrder.IDeleteInoutOrderBMO;
import com.java110.mall.cart.bmo.inoutOrder.IGetInoutOrderBMO;
import com.java110.mall.cart.bmo.inoutOrder.ISaveInoutOrderBMO;
import com.java110.mall.cart.bmo.inoutOrder.IUpdateInoutOrderBMO;
import com.java110.dto.inoutOrder.InoutOrderDto;
import com.java110.po.inoutOrder.InoutOrderPo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping(value = "/inoutOrder")
public class InoutOrderApi {

    @Autowired
    private ISaveInoutOrderBMO saveInoutOrderBMOImpl;
    @Autowired
    private IUpdateInoutOrderBMO updateInoutOrderBMOImpl;
    @Autowired
    private IDeleteInoutOrderBMO deleteInoutOrderBMOImpl;

    @Autowired
    private IGetInoutOrderBMO getInoutOrderBMOImpl;

    /**
     * 保存出库
     *
     * @param reqJson
     * @return
     * @serviceCode /inoutOrder/saveOrderOut
     * @path /app/inoutOrder/saveOrderOut
     */
    @RequestMapping(value = "/saveOrderOut", method = RequestMethod.POST)
    public ResponseEntity<String> saveOrderOut(@RequestBody JSONObject reqJson,@RequestHeader(value = "store-id") String storeId) {

        Assert.hasKeyAndValue(reqJson, "orderType", "请求报文中未包含orderType");
        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含商铺");


        return updateInoutOrderBMOImpl.saveOrderOut(reqJson,storeId);
    }

    /**
     * 微信修改消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /inoutOrder/saveOrderIn
     * @path /app/inoutOrder/saveOrderIn
     */
    @RequestMapping(value = "/saveOrderIn", method = RequestMethod.POST)
    public ResponseEntity<String> saveOrderIn(@RequestHeader(value = "store-id") String storeId,@RequestBody JSONObject reqJson
    ) {
        Assert.hasKeyAndValue(reqJson, "orderType", "请求报文中未包含orderType");
        Assert.hasKeyAndValue(reqJson, "personId", "请求报文中未包含personId");
        Assert.hasKeyAndValue(reqJson, "personName", "请求报文中未包含personName");
        Assert.hasKeyAndValue(reqJson, "orderId", "orderId不能为空");

        //InoutOrderPo inoutOrderPo = BeanConvertUtil.covertBean(reqJson, InoutOrderPo.class);
        return updateInoutOrderBMOImpl.saveOrderIn(reqJson,storeId);
    }

    /**
     * 审核 进货单
     *
     * @param reqJson
     * @return
     * @serviceCode /inoutOrder/auditInoutOrder
     * @path /app/inoutOrder/auditInoutOrder
     */
    @RequestMapping(value = "/auditInoutOrder", method = RequestMethod.POST)
    public ResponseEntity<String> auditInoutOrder(@RequestBody JSONObject reqJson, @RequestHeader(value = "user-id") String userId) {

        Assert.hasKeyAndValue(reqJson, "orderId", "请求报文中未包含订单");
        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含商铺");
        Assert.hasKeyAndValue(reqJson, "state", "请求报文中未包含状态");

        reqJson.put("personId", userId);


        return saveInoutOrderBMOImpl.audit(reqJson);
    }

    /**
     * 微信修改消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /inoutOrder/updateInoutOrder
     * @path /app/inoutOrder/updateInoutOrder
     */
    @RequestMapping(value = "/updateInoutOrder", method = RequestMethod.POST)
    public ResponseEntity<String> updateInoutOrder(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "orderType", "请求报文中未包含orderType");
        Assert.hasKeyAndValue(reqJson, "personId", "请求报文中未包含personId");
        Assert.hasKeyAndValue(reqJson, "personName", "请求报文中未包含personName");
        Assert.hasKeyAndValue(reqJson, "orderId", "orderId不能为空");


        InoutOrderPo inoutOrderPo = BeanConvertUtil.covertBean(reqJson, InoutOrderPo.class);
        return updateInoutOrderBMOImpl.update(inoutOrderPo);
    }


    /**
     * 微信删除消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /inoutOrder/deleteInoutOrder
     * @path /app/inoutOrder/deleteInoutOrder
     */
    @RequestMapping(value = "/deleteInoutOrder", method = RequestMethod.POST)
    public ResponseEntity<String> deleteInoutOrder(@RequestBody JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "shopId", "商铺不能为空");

        Assert.hasKeyAndValue(reqJson, "orderId", "orderId不能为空");


        InoutOrderPo inoutOrderPo = BeanConvertUtil.covertBean(reqJson, InoutOrderPo.class);
        return deleteInoutOrderBMOImpl.delete(inoutOrderPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param shopId 商铺ID
     * @return
     * @serviceCode /inoutOrder/queryInoutOrder
     * @path /app/inoutOrder/queryInoutOrder
     */
    @RequestMapping(value = "/queryInoutOrder", method = RequestMethod.GET)
    public ResponseEntity<String> queryInoutOrder(@RequestParam(value = "shopId",required = false) String shopId,
                                                  @RequestParam(value = "orderType",required = false) String orderType,
                                                  @RequestParam(value = "personName",required = false) String personName,
                                                  @RequestParam(value = "personNameLike",required = false) String personNameLike,
                                                  @RequestParam(value = "prodNameLike",required = false) String prodNameLike,
                                                  @RequestParam(value = "orderId",required = false) String orderId,
                                                  @RequestParam(value = "state",required = false) String state,
                                                  @RequestParam(value = "page") int page,
                                                  @RequestParam(value = "row") int row) {
        InoutOrderDto inoutOrderDto = new InoutOrderDto();
        inoutOrderDto.setPage(page);
        inoutOrderDto.setRow(row);
        inoutOrderDto.setShopId(shopId);
        inoutOrderDto.setState(state);
        inoutOrderDto.setOrderType(orderType);
        inoutOrderDto.setPersonName(personName);
        inoutOrderDto.setOrderId(orderId);
        inoutOrderDto.setPersonNameLike(personNameLike);
        inoutOrderDto.setProdNameLike(prodNameLike);
        return getInoutOrderBMOImpl.get(inoutOrderDto);
    }
}
